unit Candidate; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, StdCtrls;

type
  
  { TCandidateForm }

  TCandidateForm = class ( TForm ) 
   Button1 : TButton;
   bxRace : TEdit;
   bxTitle : TEdit;
   bxName : TEdit;
   bxSurname : TEdit;
   bxAge : TEdit;
   bxGender : TEdit;
   bxLanguage : TEdit;
   lblGender : TLabel;
   lblRace : TLabel;
   lblLanguage : TLabel;
   lblTitle : TLabel;
   lblName : TLabel;
   lblSurname : TLabel;
   lblAge : TLabel;
   Panel10 : TPanel;
   Panel2 : TPanel;
   Panel3 : TPanel;
   Panel4 : TPanel;
   Panel5 : TPanel;
   Panel6 : TPanel;
   Panel7 : TPanel;
   Panel8 : TPanel;
   Panel9 : TPanel;
   procedure Button1Click ( Sender : TObject ) ;
   procedure FormCloseQuery ( Sender : TObject; var CanClose : boolean ) ;
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  CandidateForm : TCandidateForm; 

implementation

function min(a,b:integer):integer;
begin
  if a > b then min := b else min := a;
end;

{ TCandidateForm }

procedure TCandidateForm.FormCloseQuery ( Sender : TObject; var CanClose : boolean ) ;
var s : string;
    spot : integer;
    shift : TShiftState;
begin
  shift := GetKeyShiftState;
  CanClose := true;
  if ssCtrl in Shift then exit;
  
  // Check if all information has valid inputs
  Spot := 100;
  
  
  CanClose := CanClose and (bxTitle.text <> '');
  if bxTitle.text = '' then Spot := min(spot,1);
  
  CanClose := CanClose and (bxName.text <> '');
  if bxName.text = '' then Spot := min(spot,2);
  
  CanClose := CanClose and (bxSurname.text <> '');
  if bxSurname.text = '' then Spot := min(spot,3);
  
  CanClose := CanClose and (strtointdef(bxAge.text,0) >= 16);
  if strtointdef(bxAge.text,0) < 16 then Spot := min(spot,4);
  
  CanClose := CanClose and (
    (lowercase(bxGender.text) = 'male') or
    (lowercase(bxGender.text) = 'm') or
    (lowercase(bxGender.text) = 'female') or
    (lowercase(bxGender.text) = 'f'));
  if not (
    (lowercase(bxGender.text) = 'male') or
    (lowercase(bxGender.text) = 'm') or
    (lowercase(bxGender.text) = 'female') or
    (lowercase(bxGender.text) = 'f')) then spot := min(spot,5);
  
  CanClose := CanClose and (bxRace.text <> '');
  if bxRace.text = '' then Spot := min(spot,6);
    
  CanClose := CanClose and (bxLanguage.text <> '');
  if bxLanguage.text = '' then Spot := min(spot,7);
    
  
  case spot of 
  1: S := 'Please enter your title';
  2: S := 'Please enter your name';
  3: S := 'Please enter your surname';
  4: S := 'Please enter your age';
  5: S := 'Please enter your gender'; 
  6: S := 'Please enter your ethnicity';
  7: S := 'Please enter your language';  
  else S := '';
  end;

  If Not CanClose then 
    MessageDlg('Information missing',S,mtInformation,[mbOk],0);
end;

procedure TCandidateForm.Button1Click ( Sender : TObject ) ;
begin
  // Can Close will check if it may by closed.
  Close;
end;

initialization
  {$I candidate.lrs}

end.

